package exttable.timetable.groupshchemapanel;

import com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException;
import dbutils.DbUtils;
import exttable.dbclasses.Group;
import exttable.dbclasses.GroupsSchemaVisitsId;
import exttable.formgenerator.ComponentsToPanel;
import exttable.formgenerator.MessageFactory;
import exttable.formgenerator.StaticMethods;
import utils.Localisation;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: ADMIN
 * Date: 10.05.2011
 * Time: 9:29:23
 * To change this template use File | Settings | File Templates.
 */
public class GroupsSchemaFrame {
    private JFrame frame;
    private JPanel panel;
    private JComboBox groupComboBox;
    private JScrollPane jsp;
    private JList list;
    private JButton saveButton;
    private List<GroupsSchemaVisitsId> groupsSchemaVisitsIdList;
    private String query="INSERT INTO groups_test_type(group_id, groups_schema_visits_id) VALUES ";
    private String values="";
    private int count;
    public GroupsSchemaFrame(){
        frame = new JFrame();
        panel =new JPanel();
        try{
        groupComboBox =new JComboBox(StaticMethods.getGroupList().toArray());
        }catch (Exception e){
            e.printStackTrace();
        }
        panel.add(groupComboBox);
        groupsSchemaVisitsIdList=StaticMethods.getGroupsSchemaVisitsIdList();
        list = new JList(groupsSchemaVisitsIdList.toArray());
        jsp=new JScrollPane(list);
        panel.add(jsp);
        saveButton = new JButton(Localisation.getMessage("error.save"));
        saveButton.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                MessageFactory.isToSave();
                Group group =(Group)groupComboBox.getSelectedItem();
                count=0;
                values="";
                for(Object obj:list.getSelectedValuesList()){
                    count++;
                   GroupsSchemaVisitsId gsvi=(GroupsSchemaVisitsId) obj;
                    if(count==1){
                        values=values+"("+group.getGroupId()+", "+gsvi.getGroupsSchemaVisitsId()+")";
                      }
                      else{
                       values=values+", ("+group.getGroupId()+", "+gsvi.getGroupsSchemaVisitsId()+")";
                      }
                }

                try{
                    Connection conn= DbUtils.getDBConnectionGeneral();
                    PreparedStatement ps = conn.prepareStatement(query+values);
                if (ps.executeUpdate() > 0) {
                    JOptionPane.showMessageDialog(null, Localisation.getMessage("info.dataadded"), "", JOptionPane.INFORMATION_MESSAGE);
                    frame.dispose();

                }
                conn.close();
            } catch (MySQLIntegrityConstraintViolationException excep) {


                JOptionPane.showMessageDialog(null, ComponentsToPanel.getDuplicateMessage(excep.getMessage()), "error",
                        JOptionPane.ERROR_MESSAGE);
                excep.printStackTrace();
            } catch (Exception excep) {
                JOptionPane.showMessageDialog(null, Localisation.getMessage("error.applicationerror"), "error",
                        JOptionPane.ERROR_MESSAGE);
                excep.printStackTrace();
            }
            }
        });
        panel.add(saveButton);
        frame.add(panel);
        frame.setVisible(true);
        frame.setSize(800,250);
        organizeLayout();
    }

    public void organizeLayout(){
        SpringLayout springLayout = new SpringLayout();
        panel.setLayout(springLayout);

        springLayout.putConstraint(SpringLayout.WEST, groupComboBox, 10, SpringLayout.WEST ,frame);
        springLayout.putConstraint(SpringLayout.NORTH, groupComboBox, 10, SpringLayout.NORTH ,frame);
        springLayout.putConstraint(SpringLayout.WEST, jsp, 20, SpringLayout.EAST, groupComboBox);
        springLayout.putConstraint(SpringLayout.NORTH, jsp, 10, SpringLayout.NORTH, frame);
        springLayout.putConstraint(SpringLayout.WEST, saveButton, 20, SpringLayout.EAST, groupComboBox);
        springLayout.putConstraint(SpringLayout.NORTH, saveButton, 5, SpringLayout.SOUTH, jsp);
    }
}
